/* Copyright (C) 2006 - 2008 ScriptDev2 <https://scriptdev2.svn.sourceforge.net/>
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "ScriptPCH.h"
#include "ObjectMgr.h"
#include "custom_defines.h"
#include <cstring>

//This function is called when the player opens the gossip menubool
bool GossipHello_custom_teleporter(Player *pPlayer, Creature* pCreature)
{
    // The player is in combat and hes not a GM :O
    if (pPlayer->isInCombat() == true && pPlayer->isGameMaster() == false)
    {
        ChatHandler(pPlayer).PSendSysMessage("Your in combat!");
        return false;
    }
    // Hes gm lets show him both teleport locations
    if (pPlayer->isGameMaster())
    {
        pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Teleport to HShopping Mall",GOSSIP_SENDER_MAIN,1,"",0);
        pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Teleport to AShopping Mall",GOSSIP_SENDER_MAIN,2,"",0);
        pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Teleport to HPvPZone",GOSSIP_SENDER_MAIN,3,"",0);
        pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Teleport to APvPZone",GOSSIP_SENDER_MAIN,4,"",0);
    }
    // Or he were horde so lets just allow him to go to Thrallmar
    else if (pPlayer->GetTeam() == HORDE)
    {
        pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Teleport to Shopping Mall",GOSSIP_SENDER_MAIN,1,"",0);
        pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Teleport to PvPZone",GOSSIP_SENDER_MAIN,3,"",0);
    }
    // This bastard is ally, hes going for Honor Hold
    else
    {
        pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Teleport to Shopping Mall",GOSSIP_SENDER_MAIN,2,"",0);
        pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Teleport to PvPZone",GOSSIP_SENDER_MAIN,4,"",0);
    }
    // He might wanna reset his talents?
    pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Reset Talent Points",GOSSIP_SENDER_MAIN,9999,"",0);
    pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Change name",GOSSIP_SENDER_MAIN,9998,"",0);

    pPlayer->PlayerTalkClass->SendGossipMenu(907,pCreature->GetGUID());
    return true;
}

//This function is called when the player clicks an option on the gossip menubool
bool GossipSelect_custom_teleporter(Player *pPlayer, Creature* pCreature, uint32 sender, uint32 action)
{
    // Teleport to Thrallmar
    if (action == 1)
    {
            pPlayer->TeleportTo(530,214.473511,2693.708740,90.702003,4.778910,0);
            pPlayer->CLOSE_GOSSIP_MENU();
            ChatHandler(pPlayer).PSendSysMessage("%s[Transportation Service]%s Welcome to Thrallmar",MSG_COLOR_MAGENTA,MSG_COLOR_WHITE);
    }
    // Teleport to Honor Hold
    if (action == 2)
    {
        pPlayer->TeleportTo(530,-806.848328,2619.057617,124.389679,3.718702,0);
        pPlayer->CLOSE_GOSSIP_MENU();
        ChatHandler(pPlayer).PSendSysMessage("%s[Transportation Service]%s Welcome to Honor Hold",MSG_COLOR_MAGENTA,MSG_COLOR_WHITE);
    }
    // Horde PvP
    if (action == 3)
    {
        pPlayer->TeleportTo(530,4119.065918,2928.633057,354.522827,1.628051,0);
        pPlayer->CLOSE_GOSSIP_MENU();
        ChatHandler(pPlayer).PSendSysMessage("%s[Transportation Service]%s Welcome to PvP Zone",MSG_COLOR_MAGENTA,MSG_COLOR_WHITE);
    }
    // Alliance PvP
    if (action == 4)
    {
        pPlayer->TeleportTo(530,4199.666016,3098.937012,335.809433,4.074731,0);
        pPlayer->CLOSE_GOSSIP_MENU();
        ChatHandler(pPlayer).PSendSysMessage("%s[Transportation Service]%s Welcome to PvP Zone",MSG_COLOR_MAGENTA,MSG_COLOR_WHITE);
    }
    // Change name
    if (action == 9998)
    {
        pPlayer->SetAtLoginFlag(AT_LOGIN_RENAME);
        pPlayer->CLOSE_GOSSIP_MENU();
        ChatHandler(pPlayer).PSendSysMessage("%s[Character Service]%s You will be asked to change your name on next login",MSG_COLOR_MAGENTA,MSG_COLOR_WHITE);
    }
    // Reset talents
    if (action == 9999)
    {
        pPlayer->resetTalents();
        pPlayer->CLOSE_GOSSIP_MENU();
        ChatHandler(pPlayer).PSendSysMessage("%s[Character Service]%s Your Talents have been reset",MSG_COLOR_MAGENTA,MSG_COLOR_WHITE);
    }

    return true;
}

//This function is called when the player opens the gossip menubool
bool GossipHello_hunter_pet(Player *pPlayer, Creature* pCreature)
{
    // The player is in combat and hes not a GM :O
    if (pPlayer->isInCombat() == true && pPlayer->isGameMaster() == false)
    {
        ChatHandler(pPlayer).PSendSysMessage("Your in combat!");
        return false;
    }
    if (pPlayer->getClass() == CLASS_HUNTER)
    {
        pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Target a beast standing here around me and then click here",GOSSIP_SENDER_MAIN,1,"",0);
        pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"If your pet is sad click here",GOSSIP_SENDER_MAIN,2,"",0);
    }
    else
    {
        pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"You are no hunter, i'm sorry i dont have time.");
    }
    pPlayer->PlayerTalkClass->SendGossipMenu(907,pCreature->GetGUID());
    return true;
}

//This function is called when the player clicks an option on the gossip menubool
bool GossipSelect_hunter_pet(Player *pPlayer, Creature* pCreature, uint32 sender, uint32 action)
{
    if (action == 1)
    {
        Creature *creatureTarget = ChatHandler(pPlayer).getSelectedCreature();

        if (!creatureTarget || creatureTarget->isPet() || creatureTarget->GetTypeId() == TYPEID_PLAYER)
        {
            ChatHandler(pPlayer).PSendSysMessage("You need to target a creature");
            return false;
        }

        CreatureInfo const* cInfo = objmgr.GetCreatureTemplate(creatureTarget->GetEntry());
        // Creatures with family 0 crashes the server
        if (cInfo->family == 0)
        {
            ChatHandler(pPlayer).PSendSysMessage("This creature cannot be tamed. (family id: 0).");
            return false;
        }

        if (pPlayer->GetPetGUID())
        {
            ChatHandler(pPlayer).PSendSysMessage("You already have a pet");
            return false;
        }

        // Everything looks OK, create new pet
        Pet* pet = new Pet(pPlayer, HUNTER_PET);

        if (!pet)
          return false;

        if (!pet->CreateBaseAtCreature(creatureTarget))
        {
            delete pet;
            ChatHandler(pPlayer).PSendSysMessage("Error 1");
            return false;
        }

        creatureTarget->setDeathState(JUST_DIED);
        creatureTarget->RemoveCorpse();
        creatureTarget->SetHealth(0); // just for nice GM-mode view

        pet->SetUInt64Value(UNIT_FIELD_CREATEDBY, pPlayer->GetGUID());
        pet->SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE, pPlayer->getFaction());

        if (!pet->InitStatsForLevel(creatureTarget->getLevel()))
        {
            sLog.outError("InitStatsForLevel() in EffectTameCreature failed! Pet deleted.");
            ChatHandler(pPlayer).PSendSysMessage("Error 2");
            delete pet;
            return false;
        }

        // prepare visual effect for levelup
        pet->SetUInt32Value(UNIT_FIELD_LEVEL,creatureTarget->getLevel()-1);

        pet->GetCharmInfo()->SetPetNumber(objmgr.GeneratePetNumber(), true);
        // this enables pet details window (Shift+P)
        pet->InitPetCreateSpells();
        pet->SetHealth(pet->GetMaxHealth());

        pet->GetMap()->Add(pet->ToCreature());

        // visual effect for levelup
        pet->SetUInt32Value(UNIT_FIELD_LEVEL,creatureTarget->getLevel());

        pPlayer->SetMinion(pet, true);
        pet->SavePetToDB(PET_SAVE_AS_CURRENT);
        pPlayer->PetSpellInitialize();

        return true;
        // Greater Shadowbat
        if (creatureTarget->GetEntry() == 16174)
        {
            //PetLearn(pPlayer, spellid);
        }
        // Underbat
        if (creatureTarget->GetEntry() == 17724)
        {

        }
        // Bloodfalcon
        if (creatureTarget->GetEntry() == 18155)
        {

        }
        // Amani Crocolisk
        if (creatureTarget->GetEntry() == 24047)
        {

        }
        // Blackwind Warp Chaser
        if (creatureTarget->GetEntry() == 23219)
        {

        }
        // Blackwind Sabercat
        if (creatureTarget->GetEntry() == 21723)
        {

        }
        // Amani Elder Lynx
        if (creatureTarget->GetEntry() == 24531)
        {

        }

        return true;
    }
    else if (action == 2)
    {
        if (pPlayer->GetPetGUID())
        {
            pPlayer->GetPet()->SetTP(1500);
            pPlayer->GetPet()->SetPower(POWER_HAPPINESS, 1048000);
            pPlayer->GetPet()->SetLoyaltyLevel(BEST_FRIEND);
            ChatHandler(pPlayer).PSendSysMessage("Your pet shall be happy again now");
            return true;
        }
        else
        {
            ChatHandler(pPlayer).PSendSysMessage("You dont have any pet....");
            return false;
        }
    }


    return true;
}

bool PetLearn(Player *pPlayer, uint32 spellId)
{
    if (!spellId)
        return false;
    Pet *pPet = pPlayer->GetPet();
    if (!spellId || !sSpellStore.LookupEntry(spellId))
        return false;
    // Check if pet already has it
    if (pPet->HasSpell(spellId))
    {
        ChatHandler(pPlayer).PSendSysMessage("Pet already has spell: %u", spellId);
        return false;
    }
    // Check if spell is valid
    SpellEntry const* spellInfo = sSpellStore.LookupEntry(spellId);
    if (!spellInfo || !SpellMgr::IsSpellValid(spellInfo))
    {
        ChatHandler(pPlayer).PSendSysMessage("The spell %u didnt want to learn Q_Q",spellId);
        return false;
    }
    pPet->learnSpell(spellId);
    return true;
}

bool GossipHello_hide_seek(Player *pPlayer, Creature* pCreature)
{
    pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Congratulations you found me",GOSSIP_SENDER_MAIN,1,"",0);
    pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Click here to claim your gold",GOSSIP_SENDER_MAIN,1,"",0);
    pPlayer->PlayerTalkClass->SendGossipMenu(907,pCreature->GetGUID());
    return true;
}
bool GossipSelect_hide_seek(Player *pPlayer, Creature* pCreature, uint32 sender, uint32 action)
{
    if (action == 1)
    {
        ChatHandler(pPlayer).PSendSysMessage("There you got some gold, ciao some time");
        uint32 currgold = pPlayer->GetUInt32Value(PLAYER_FIELD_COINAGE);
        pPlayer->SetUInt32Value(PLAYER_FIELD_COINAGE,currgold+((urand(50, 100))*10000));
        pCreature->CombatStop();
        pCreature->DeleteFromDB();
        pCreature->AddObjectToRemoveList();
        return true;
    }
    return false;
}

bool ItemUse_item_gossip(Player* pPlayer, Item* pItem, const SpellCastTargets &pTargets)
{
    extern DatabaseType WorldDatabase;
    pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Hearthstone"       ,1,1,"",0);
    pPlayer->PlayerTalkClass->GetGossipMenu().AddMenuItem(0,"Repair/Misc"       ,2,1,"",0);
    pPlayer->PlayerTalkClass->SendGossipMenu(907,0);
    return false;
}
bool GossipSelect_item_gossip(Player *pPlayer, Creature* pCreature, uint32 sender, uint32 action)
{
    if (action == 1)
    {
        pPlayer->CastSpell(pPlayer, 8690, true, NULL, NULL, 0);
        return true;
    }
    else if (action == 2)
    {
        pPlayer->SummonCreature(81027,pPlayer->GetPositionX() ,pPlayer->GetPositionY()-2, pPlayer->GetPositionZ()+3, 0, TEMPSUMMON_TIMED_DESPAWN, 120000);
        return true;
    }
    return false;
}
void AddSC_custom_gossip()
{
    Script *newscript;

    newscript = new Script;
    newscript->Name = "custom_teleporter";
    newscript->pGossipHello          = &GossipHello_custom_teleporter;
    newscript->pGossipSelect         = &GossipSelect_custom_teleporter;
    newscript->RegisterSelf();

    newscript = new Script;
    newscript->Name = "hunter_pet";
    newscript->pGossipHello          = &GossipHello_hunter_pet;
    newscript->pGossipSelect         = &GossipSelect_hunter_pet;
    newscript->RegisterSelf();

    newscript = new Script;
    newscript->Name = "hide_seek";
    newscript->pGossipHello          = &GossipHello_hide_seek;
    newscript->pGossipSelect         = &GossipSelect_hide_seek;
    newscript->RegisterSelf();

    newscript = new Script;
    newscript->Name = "item_hearthstone";
    newscript->pItemUse              = &ItemUse_item_gossip;
    newscript->pGossipSelect         = &GossipSelect_item_gossip;
    newscript->RegisterSelf();
}
